﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using DiscoveryLogic.Analytics.Modules.Diagrams.GravityDiagrams;
using System.Windows.Media;

namespace DiscoveryLogic.Analytics.Modules.Diagrams.Renders
{
    /// <summary>
    /// zig-zag line
    /// </summary>
    public class BladeSpring : ISpringRenderer
    {
        #region props
        private Pen _Pen;
        /// <summary></summary>
        public Pen Pen
        {
            get
            {
                return this._Pen;
            }
            set
            {
                this._Pen = value;
            }
        }

        private double _Width;
        /// <summary></summary>
        public double Width
        {
            get
            {
                return this._Width;
            }
            set
            {
                this._Width = value;
            }
        }

        private uint _Turns;
        /// <summary></summary>
        public uint Turns
        {
            get
            {
                return this._Turns;
            }
            set
            {
                this._Turns = value;
            }
        }
        #endregion

        #region ctor
        public BladeSpring(Pen pen, double width, uint turns)
        {
            this._Pen = pen;
            this._Width = width;
            this._Turns = turns;
        }
        #endregion

        #region Implementation of ISpringRenderer

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spring"></param>
        /// <param name="dc"></param>
        public void Render(Spring spring, DrawingContext dc)
        {
            Vector direction = spring.To.Position - spring.From.Position;
            Vector angleVector = new Vector(-direction.Y, direction.X);
            double distance = (direction.Length * 0.9) / this._Turns;
            double offsetDistance = direction.Length * 0.1;
            direction.Normalize();
            angleVector.Normalize();

            Vector prevPoint = spring.From.Position;
            for (uint i = 0; i < this._Turns; i++)
            {
                Vector point = spring.From.Position +
                               (direction * (offsetDistance + distance * i) + (angleVector * (this._Width * ((i & 1) == 0 ? 1.0 : -1.0))));
                dc.DrawLine(this._Pen, prevPoint.ToPoint(), point.ToPoint());
                prevPoint = point;
            }

            dc.DrawLine(this._Pen, prevPoint.ToPoint(), spring.To.Position.ToPoint());
        }

        #endregion
    }
}
